package demo2;

class Money implements Cloneable{
    public double m = 12.5;
      //通过这个方法无法实现深拷贝 因为无人调用它
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable {
    public int id;
    public Money money = new Money();//组合

    @Override
    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
        Person tmp = (Person) super.clone();
        //得在Person类中调用money的clone方法
        tmp.money = (Money)this.money.clone();
        return tmp;
    }

    @Override
    public String toString() {
        return "person" + id + this.money.m;
    }
}


public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person2 = (Person)person.clone();
        person2.money.m = 1999;
        System.out.println("1"+person);

        System.out.println("2"+person2);
    }




}
